package com.hoshiicloud.elasticsearch.controller;


import com.hoshiicloud.elasticsearch.entity.EsProductRelatedInfo;
import com.hoshiicloud.elasticsearch.entity.Spu;
import com.hoshiicloud.elasticsearch.service.SpuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 搜索商品管理Controller
 */
@Controller
@Api(tags = "SpuController", description = "搜索商品管理")
@RequestMapping("/spu")
public class SpuController {
    @Autowired
    private SpuService spuService;

    @ApiOperation(value = "导入所有数据库中商品到ES")
    @RequestMapping(value = "/importSpuAll", method = RequestMethod.POST)
    @ResponseBody
    public Integer importSpuAllList() {
        int count = spuService.importSpuAll();
        return count;
    }

    @ApiOperation(value = "根据id删除商品")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public void delete(@PathVariable Long id) {
        spuService.delete(id);
    }

    @ApiOperation(value = "根据id批量删除商品")
    @RequestMapping(value = "/delete/batch", method = RequestMethod.POST)
    @ResponseBody
    public void delete(@RequestParam("ids") List<Long> ids) {
        spuService.delete(ids);
    }

    @ApiOperation(value = "根据id创建商品")
    @RequestMapping(value = "/create/{id}", method = RequestMethod.POST)
    @ResponseBody
    public boolean create(@PathVariable Long id) {
        Spu esProduct = spuService.create(id);
        if (esProduct != null) {
            return true;
        } else {
            return false;
        }
    }

    @ApiOperation(value = "查询商品列表")
    @RequestMapping(value = "/search/list", method = RequestMethod.GET)
    @ResponseBody
    public com.baomidou.mybatisplus.extension.plugins.pagination.Page<Spu> list(@RequestParam Long storeId,
                                                                                @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                                                @RequestParam(required = false, defaultValue = "6") Integer pageSize) {
        Page<Spu> spuPage = spuService.list(storeId, pageNum, pageSize);

        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Spu> spuPage1 = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        spuPage1.setRecords(spuPage.getContent());
        spuPage1.setTotal(spuPage.getTotalElements());
        spuPage1.setSize(spuPage.getSize());
        spuPage1.setCurrent(spuPage.getNumber()+1);
        spuPage1.setPages(spuPage.getTotalPages());
        return spuPage1;
    }

    @ApiOperation(value = "简单搜索")
    @RequestMapping(value = "/search/simple", method = RequestMethod.GET)
    @ResponseBody
    public com.baomidou.mybatisplus.extension.plugins.pagination.Page<Spu> search(@RequestParam(required = false) String keyword,
                                                                                  @RequestParam Long storeId,
                                                                                  @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                                                                                  @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<Spu> spuPage = spuService.search(keyword, storeId, pageNum, pageSize);
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<Spu> spuPage1 = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>();
        spuPage1.setRecords(spuPage.getContent());
        spuPage1.setTotal(spuPage.getTotalElements());
        spuPage1.setSize(spuPage.getSize());
        spuPage1.setCurrent(spuPage.getNumber()+1);
        spuPage1.setPages(spuPage.getTotalPages());
        return spuPage1;
    }

    @ApiOperation(value = "综合搜索、筛选、排序")
    @ApiImplicitParam(name = "sort", value = "排序字段:0->按相关度；1->按新品；2->按销量；3->价格从低到高；4->价格从高到低",
            defaultValue = "0", allowableValues = "0,1,2,3,4", paramType = "query", dataType = "integer")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ResponseBody
    public Page<Spu> search(@RequestParam(required = false) String keyword,
                            @RequestParam(required = false) Long brandId,
                            @RequestParam(required = false) Long productCategoryId,
                            @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                            @RequestParam(required = false, defaultValue = "5") Integer pageSize,
                            @RequestParam(required = false, defaultValue = "0") Integer sort) {
        Page<Spu> spuPage = spuService.search(keyword, brandId, productCategoryId, pageNum, pageSize, sort);
        return spuPage;
    }

    @ApiOperation(value = "根据商品id推荐商品")
    @RequestMapping(value = "/recommend/{id}", method = RequestMethod.GET)
    @ResponseBody
    public Page<Spu> recommend(@PathVariable Long id,
                               @RequestParam(required = false, defaultValue = "0") Integer pageNum,
                               @RequestParam(required = false, defaultValue = "5") Integer pageSize) {
        Page<Spu> spuPage = spuService.recommend(id, pageNum, pageSize);
        return spuPage;
    }

    @ApiOperation(value = "获取搜索的相关品牌、分类及筛选属性")
    @RequestMapping(value = "/search/relate", method = RequestMethod.GET)
    @ResponseBody
    public EsProductRelatedInfo searchRelatedInfo(@RequestParam(required = false) String keyword) {
        EsProductRelatedInfo productRelatedInfo = spuService.searchRelatedInfo(keyword);
        return productRelatedInfo;
    }
}
